<?php
	class Habit_category extends MY_controller{

		function __construct(){
			parent::__construct();
		}

		function index($name = 0, $limit = 0){
			if($this->input->post()){
				$limit = 0;
				$post_data = $this->input->post();
				$name = $post_data['name'] ? urldecode($post_data['name']) : 0;
			}
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$conditions = array('name' => $name);

			$this->load->model("Habit_category_model");

			$result = $this->Habit_category_model->get_type_all();
			

			$this->smarty->assign("action", '习惯类型');
			$this->smarty->assign("name", '');
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("list", $result);
			$this->smarty->assign("pagination", '');
			$this->smarty->display("task_type/list.html");
		}

		function add(){
			$post_data = $this->input->post();
			if($post_data){
				if(isset($post_data['file'])) unset($post_data['file']);
				if(isset($post_data['image'])) $post_data['image'] = get_qiniu_url($post_data['image']);
				$post_data['add_time'] = time();
				if(!isset($post_data['status'])) $post_data['status'] = 0;

				//添加数据库
				$this->db->insert("habit_category", $post_data);
				$insert_id = $this->db->insert_id();
				if($insert_id > 0){
 					show_message($_SERVER['HTTP_REFERER']);
 				}else{
 					show_message($_SERVER['HTTP_REFERER'], "操作失败");
 				}
			}else{
				// $this->load->model("Tk_categorys");
				$this->load->model("Habit_category_model");

				$result = $this->Habit_category_model->get_type_by_pid(0);
				$this->smarty->assign("list", $result);

				$this->smarty->assign("action", '添加任务类型');
				$this->smarty->assign("post_url", '/task_type/add');
				$this->smarty->display("task_type/save.html");
			}
		}

		function edit($id = 0){
			$post_data = $this->input->post();
			if($post_data){
				if(!isset($post_data['status'])) $post_data['status'] = 0;
				if(isset($post_data['file'])) unset($post_data['file']);
				if(isset($post_data['image'])) $post_data['image'] = get_qiniu_url($post_data['image']);
				$this->db->where('id', $post_data['id']);
 				$this->db->update('task_types', $post_data);
 				if($this->db->affected_rows() > 0){
 					show_message($_SERVER['HTTP_REFERER']);
 				}else{
 					show_message($_SERVER['HTTP_REFERER'], "操作失败");
 				}
			}else{
				if(intval($id) > 0){
					$tk_category_info = get_field_by_where("task_types", "*", "id='{$id}'");
					if(!$tk_category_info){
						show_message("/task_type", "数据不存在");exit;
					}
					// print_r($tk_category_info);exit;
					$this->smarty->assign("tk_category_info", $tk_category_info);
					
					$this->smarty->assign("action", '编辑任务类型');
					$this->smarty->assign("post_url", '/task_type/edit');
					$this->smarty->display("task_type/save.html");
				}else{
					show_message("/task_type", "非法操作");
				}
			}
		}

		function delete($ids = 0){
			if($_POST){
				$ids = $this->input->post('ids');
			}else{
				$ids = (array) $ids;
			}
			delete_data($ids, "task_types");
			if($this->db->affected_rows() > 0){
				show_message($_SERVER['HTTP_REFERER'], "删除成功");
			}else{
				show_message($_SERVER['HTTP_REFERER'], "删除失败，请重试！");
			}
		}

	
	}